package com.xiaoq.sites.repository.impl;

import com.mongodb.WriteConcern;
import com.mongodb.WriteResult;
import com.xiaoq.coms.util.ConstantUtil;
import com.xiaoq.sites.entity.InterpreterEntity;
import com.xiaoq.sites.repository.IInterpreterRepository;
import com.xiaoq.vo.PagingVo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.DeleteOptions;
import org.mongodb.morphia.query.Query;
import org.mongodb.morphia.query.UpdateOperations;
import org.mongodb.morphia.query.UpdateResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class InterpreterRepositoryImpl implements IInterpreterRepository {
	
	@Autowired
	private Datastore datastore;
	
	@Override
	public List<InterpreterEntity> findBySitesId(String sitesId,Integer status, PagingVo pagingVo) {
		if(StringUtils.isBlank(sitesId)){
			return new ArrayList<>();
		}
		Query<InterpreterEntity> interpreterEntityQuery = datastore.find(InterpreterEntity.class)
				.filter("sitesId", sitesId);
		if(status != null){
			interpreterEntityQuery.filter("status", status);
		}
		if(pagingVo != null){
			interpreterEntityQuery.offset(pagingVo.getPageOffset()).limit(pagingVo.getPs());
		}
		return interpreterEntityQuery.asList();
	}

	@Override
	public long findCountBySitesId(String sitesId, Integer status) {
		if(StringUtils.isBlank(sitesId)){
			return 0L;
		}
		Query<InterpreterEntity> interpreterEntityQuery = datastore.find(InterpreterEntity.class)
				.filter("sitesId", sitesId);
		if(status != null){
			interpreterEntityQuery.filter("status", status);
		}
		return interpreterEntityQuery.count();
	}

	@Override
	public InterpreterEntity findById(String id) {
		return datastore.get(InterpreterEntity.class, id);
	}

	@Override
	public boolean save(InterpreterEntity interpreterEntity) {
		if(interpreterEntity==null || StringUtils.isBlank(interpreterEntity.getId())){
			return false;
		}
		datastore.save(interpreterEntity);
		return true;
	}

	@Override
	public boolean update(InterpreterEntity interpreterEntity) {
		if(interpreterEntity == null || StringUtils.isBlank(interpreterEntity.getId())){
			return false;
		}
		Query<InterpreterEntity> interpreterQuery = datastore.find(InterpreterEntity.class)
				.filter("_id", interpreterEntity.getId());
		if(StringUtils.isNotBlank(interpreterEntity.getSitesId())){
			interpreterQuery.filter("sitesId", interpreterEntity.getSitesId());
		}

		UpdateOperations<InterpreterEntity> ups = datastore.createUpdateOperations(InterpreterEntity.class);
		ups.set("updatedAt", System.currentTimeMillis());

		if(StringUtils.isNotBlank(interpreterEntity.getCode())){
			ups.set("code", interpreterEntity.getCode());
		}
		if(StringUtils.isNotBlank(interpreterEntity.getEmail())){
			ups.set("email", interpreterEntity.getEmail());
		}
		if(StringUtils.isNotBlank(interpreterEntity.getName())){
			ups.set("name", interpreterEntity.getName());
		}
		if(StringUtils.isNotBlank(interpreterEntity.getImgUrl())){
			ups.set("imgUrl", interpreterEntity.getImgUrl());
		}
		if(StringUtils.isNotBlank(interpreterEntity.getLevelId())){
			ups.set("levelId", interpreterEntity.getLevelId());
		}
		if(StringUtils.isNotBlank(interpreterEntity.getInterLevel())){
			ups.set("interLevel", interpreterEntity.getInterLevel());
		}
		if(StringUtils.isNotBlank(interpreterEntity.getIdNo())){
			ups.set("idNo", interpreterEntity.getIdNo());
		}
		if(StringUtils.isNotBlank(interpreterEntity.getMobile())){
			ups.set("mobile", interpreterEntity.getMobile());
		}
		if(StringUtils.isNotBlank(interpreterEntity.getSitesId())){
			ups.set("sitesId", interpreterEntity.getSitesId());
		}
		if(StringUtils.isNotBlank(interpreterEntity.getSvcExperience())){
			ups.set("svcExperience", interpreterEntity.getSvcExperience());
		}
		if(StringUtils.isNotBlank(interpreterEntity.getWeixin())){
			ups.set("weixin", interpreterEntity.getWeixin());
		}
		if(StringUtils.isNotBlank(interpreterEntity.getUserId())){
			ups.set("userId", interpreterEntity.getUserId());
		}
		if(CollectionUtils.isNotEmpty(interpreterEntity.getSvcLang())){
			ups.set("svcLang", interpreterEntity.getSvcLang());
		}
		if(interpreterEntity.getPrice() != null){
			ups.set("price", interpreterEntity.getPrice());
		}
		if(interpreterEntity.getSex() != null){
			ups.set("sex", interpreterEntity.getSex());
		}
		if(interpreterEntity.getStatus() != null){
			ups.set("status", interpreterEntity.getStatus());
		}
		ups.set("bookNotices", interpreterEntity.getBookNotices()); //费用说明
		UpdateResults urs = datastore.updateFirst(interpreterQuery, ups,false, WriteConcern.ACKNOWLEDGED);
		return urs.getUpdatedExisting();
	}

	@Override
	public boolean delete(String id, String sitesId) {
		if(StringUtils.isAnyBlank(id, sitesId)){
			return false;
		}
		Query<InterpreterEntity> interpreterEntityQuery = datastore.createQuery(InterpreterEntity.class)
				.filter("_id", id)
				.filter("sitesId", sitesId);
		WriteResult writeResult = datastore.delete(interpreterEntityQuery, new DeleteOptions().writeConcern(WriteConcern.ACKNOWLEDGED));
		return writeResult.getN()==1;
	}

	@Override
	public boolean update2Serving(String interId, String shopId) {
		if(StringUtils.isAnyBlank(interId, shopId)){
			return false;
		}
		Query<InterpreterEntity> interpreterQuery = datastore.createQuery(InterpreterEntity.class)
				.filter("_id", interId)
				.filter("sitesId", shopId)
				.filter("status", ConstantUtil.INTERPRETER_STATUS_NOT_PAY_WAIT_SERVICE_NOT_APPOINT);

		UpdateOperations<InterpreterEntity> ups = datastore.createUpdateOperations(InterpreterEntity.class);
		ups.set("updatedAt", System.currentTimeMillis());
		ups.set("status", ConstantUtil.INTERPRETER_STATUS_SERVICE_NOT_APPOINT);
		UpdateResults urs = datastore.updateFirst(interpreterQuery, ups, false, WriteConcern.ACKNOWLEDGED);
		return urs.getUpdatedExisting();
	}

	@Override
	public boolean updateInter2CanAppoint(String interId, String shopId) {
		if(StringUtils.isAnyBlank(interId, shopId)){
			return false;
		}
		Query<InterpreterEntity> interpreterQuery = datastore.createQuery(InterpreterEntity.class)
				.filter("_id", interId)
				.filter("sitesId", shopId);

		UpdateOperations<InterpreterEntity> ups = datastore.createUpdateOperations(InterpreterEntity.class);
		ups.set("updatedAt", System.currentTimeMillis());
		ups.set("status", ConstantUtil.INTERPRETER_STATUS_WAIT_SERVICE_CAN_APPOINT);
		UpdateResults urs = datastore.updateFirst(interpreterQuery, ups, false, WriteConcern.ACKNOWLEDGED);
		return urs.getUpdatedExisting();
	}

	@Override
	public boolean updateInter2WaitService(String interId, String shopId) {
		if(StringUtils.isAnyBlank(interId, shopId)){
			return false;
		}
		Query<InterpreterEntity> interpreterQuery = datastore.createQuery(InterpreterEntity.class)
				.filter("_id", interId)
				.filter("sitesId", shopId)
				.filter("status", ConstantUtil.INTERPRETER_STATUS_WAIT_SERVICE_CAN_APPOINT);

		UpdateOperations<InterpreterEntity> ups = datastore.createUpdateOperations(InterpreterEntity.class);
		ups.set("updatedAt", System.currentTimeMillis());
		ups.set("status", ConstantUtil.INTERPRETER_STATUS_SERVICE_NOT_APPOINT);
		UpdateResults urs = datastore.updateFirst(interpreterQuery, ups, false, WriteConcern.ACKNOWLEDGED);
		return urs.getUpdatedExisting();
	}

	@Override
	public InterpreterEntity findByUserId(String userId) {
		if(StringUtils.isBlank(userId)){
			return null;
		}
		Query<InterpreterEntity> interpreterQuery = datastore.createQuery(InterpreterEntity.class)
				.filter("userId", userId);
		return interpreterQuery.get();
	}

	@Override
	public InterpreterEntity findByMobile(String mobile) {
		if(StringUtils.isBlank(mobile)){
			return null;
		}
		Query<InterpreterEntity> interpreterQuery = datastore.createQuery(InterpreterEntity.class)
				.filter("mobile", mobile);
		return interpreterQuery.get();
	}
}
